Skip to content

Run Python in a flow ​

Some image checks are easiest to write in Python, with OpenCV and NumPy. The «python-executor» node runs your Python code on each message, like a function node runs JavaScript, and hands it the image ready for OpenCV. In this guide you write a focus check: a number that drops when the camera's picture is blurred.

What you will learn

  • How to run Python code on the images of a flow
  • How to turn a Rosepetal image into an OpenCV image
  • How to keep Python running between messages so that it answers fast

Before you start

Here two inject nodes, «in focus» and «out of focus», send the path of a photo of a pack label on the controller, /tmp/line-check/label-sharp.jpg or /tmp/line-check/label-blurred.jpg, and an rp-image-in node loads it into msg.payload and shows it under itself; see Load and transform an image. The debug node «sharpness» shows msg.sharpness.

1. Add a Python node ​

Type python in «filter nodes», drag a «python executor» node between the image node and the debug node and wire it to both. Open it. With «Environment» empty it runs the controller's own Python, python3 in «Python Path», with the packages installed there; this controller has OpenCV and NumPy. «Timeout (ms)» stops code that runs too long.

Add a Python node

2. Write the check ​

Replace the example code with:

python
import cv2
gray = cv2.cvtColor(rp_to_cv(msg["payload"]), cv2.COLOR_BGR2GRAY)
# Sharp edges make the Laplacian vary a lot; a blurred picture keeps it flat
msg["sharpness"] = round(cv2.Laplacian(gray, cv2.CV_64F).var())
return msg

The code runs once per message, with the message as msg, and must return msg. rp_to_cv turns a Rosepetal image into a NumPy array in OpenCV's BGR order, and rp_from_cv turns one back, to send a processed image on. The variance of the Laplacian is a classic focus measure: sharp edges give a high value.

Write the check

3. Keep Python running ​

Tick «Hot Mode». Without it, every message starts Python again and imports OpenCV, which takes longer than the check itself. In hot mode the Python process stays alive, images reach it through shared memory, and «Workers» sets how many messages it handles at once. «Preload Imports» runs imports once when it starts.

Keep Python running

4. Deploy ​

Click «Done» and «Deploy». While «Environment» is empty the editor marks the node with a red triangle and asks you to confirm the deploy: click «Confirm deploy». The node runs with the controller's Python all the same. To keep the packages of a flow apart, create an «Environment» instead and install them from its dialog.

Deploy

5. Check a sharp picture ​

Press «in focus». The debug panel shows msg.sharpness, a high value for a picture in focus.

Check a sharp picture

6. Check a blurred picture ​

Press «out of focus». The same label out of focus gives a much lower value. Pick a limit between the two with pictures of your own line, and a switch node on msg.sharpness can warn the operators when the camera loses focus.

Check a blurred picture

Good to know ​

  • What the code can use. msg is a dictionary; flow_ctx and global_ctx read and write the flow's context, and node.warn("…") writes to the debug panel. An exception turns the status red, shows the error in the debug panel and stops the message.
  • Your own packages. In «Environment», «Add new python-environment…» creates a Python environment of its own on the controller, and its dialog installs and removes packages with pip, so two flows can use different versions.
  • Heavy JavaScript. The worker-function node is the same idea for JavaScript: it runs the code of a function node in a separate thread, so a long calculation does not hold up the rest of the flows; see Find what slows a flow down.

Next steps ​

Recorded on Rosepetal release 1.8.15, Node-RED 5.0.10.